package cn.usts.edu.lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class SubActionEvent {
    public static void main(String[] args) {
        new SubFrame();
    }
}


// 1`窗体
class SubFrame extends Frame{
    // 组件   3*文本框   1*按钮
    TextField num1, num2, num3;
    Button button1;
    Label label1;

    public SubFrame(){
        // 设置位置
        setLocation(400,400);
        setSize(200,200);
        // 设置可见
        setVisible(true);
        num1 = new TextField(10); // 字符数
        num2 = new TextField(10); // 字符数
        num3 = new TextField(10); // 字符数

        button1 = new Button("=");
        button1.addActionListener(new ActionListenerSub(num1,num2,num3)); // 动作监听
        label1 = new Label("-");
        // 布局
        setLayout(new FlowLayout());
        // 添加组件
        add(num1);
        add(label1);
        add(num2);
        add(button1);
        add(num3);

        pack();// 自适应大小

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);// 程序结束返回的状态码
            }
        });
    }
}

class ActionListenerSub implements ActionListener{

    private TextField num1,num2,num3;

    public ActionListenerSub(TextField num1,TextField num2,TextField num3) {
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int first = Integer.parseInt(num1.getText());
        int second = Integer.parseInt(num2.getText());

        num3.setText(""+(first-second));
        num1.setText("");
        num2.setText("");
    }
}




